﻿#region Using
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion Using

namespace LloydGame
{
    class ScreenMainMenu : GameScreen
    {

        #region Fields
        protected GameScreenComponent m_menu_DebugGame,
                                      m_menu_DebugNetwork,
                                      m_menu_Settings,
                                      m_menu_Multiplayer,
                                      m_menu_Quit;
        protected Texture2D m_background;
        #endregion Fields

        #region Constructor
        public ScreenMainMenu(Game p_game) : base(p_game, "Main Menu")
        {
            m_menu_DebugGame = new GameScreenComponent("Debug Game", new Vector2(80, 200), new Vector2(240, 32), p_game);
            m_menu_DebugNetwork = new GameScreenComponent("Debug Network", new Vector2(80, 240), new Vector2(240, 32), p_game);
            m_menu_Settings = new GameScreenComponent("Settings", new Vector2(80, 280), new Vector2(240, 32), p_game);
            m_menu_Multiplayer = new GameScreenComponent("Multiplayer", new Vector2(80, 320), new Vector2(240, 32), p_game);
            m_menu_Quit = new GameScreenComponent("Quit", new Vector2(80, 360), new Vector2(240, 32), p_game);

            m_selectable_components.Add(m_menu_DebugGame);
            m_selectable_components.Add(m_menu_DebugNetwork);
            m_selectable_components.Add(m_menu_Settings);
            m_selectable_components.Add(m_menu_Multiplayer);
            m_selectable_components.Add(m_menu_Quit);
            m_selected_component = 0;

            // Load background image
            m_background = Texture2D.FromFile(p_game.GraphicsDevice, "Content\\menu_background.png");
        }
        #endregion Constructor

        #region Update
        public override void Update(GameTime p_gametime)
        {
            base.Update(p_gametime);

            if (!m_rendersubscreen && m_ready)
            {
                // If mouse have moved, clear keyboard selection
                if (m_lastmouse.X != m_currentmouse.X || m_lastmouse.Y != m_currentmouse.Y)
                {

                    m_menu_DebugGame.HitTest(new Vector2(m_currentmouse.X, m_currentmouse.Y));
                    m_menu_DebugNetwork.HitTest(new Vector2(m_currentmouse.X, m_currentmouse.Y));
                    m_menu_Settings.HitTest(new Vector2(m_currentmouse.X, m_currentmouse.Y));
                    m_menu_Multiplayer.HitTest(new Vector2(m_currentmouse.X, m_currentmouse.Y));
                    m_menu_Quit.HitTest(new Vector2(m_currentmouse.X, m_currentmouse.Y));
                }

                // Debug Game?
                if (m_menu_DebugGame.Active && m_activatetrigger)
                    ((Game1)m_game).InitDebugGame();

                // Debug MultiplayerGame?
                if (m_menu_DebugNetwork.Active && m_activatetrigger)
                    this.StartSubscreen(new ScreenQuickMatch(m_game, this));

                // Settings
                if (m_menu_Settings.Active && m_activatetrigger)
                    this.StartSubscreen(new ScreenSettings(m_game, this));

                // Multiplayer -> Quick Match
                if (m_menu_Multiplayer.Active && m_activatetrigger)
                    this.StartSubscreen(new ScreenMultiplayer(m_game, this));

                // Quit?
                if (m_menu_Quit.Active && m_activatetrigger)
                    ((Game1)m_game).Exit();
            }

            // Store keyboard and mouse states
            StoreStates();
        }
        #endregion Update

        #region Draw
        public override void Draw()
        {
            // Draw background (same for all menus/screens)
            m_spriteBatch.Begin();
            m_spriteBatch.Draw(m_background, new Rectangle(0, 0, m_game.GraphicsDevice.Viewport.Width, m_game.GraphicsDevice.Viewport.Height), Color.White);
            m_spriteBatch.End();

            if (!m_rendersubscreen)
            {
                m_spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, m_screenmatrix);

                // Draw menu components
                m_menu_DebugGame.Draw(m_spriteBatch, false);
                m_menu_DebugNetwork.Draw(m_spriteBatch, false);
                m_menu_Settings.Draw(m_spriteBatch, false);
                m_menu_Multiplayer.Draw(m_spriteBatch, false);
                m_menu_Quit.Draw(m_spriteBatch, false);

                m_spriteBatch.End();
            }
            else
            {
                m_subscreen.Draw();
            }

            base.Draw();
        }
        #endregion Draw

    }
}
